<?php

namespace App\Http\Resources\Article;

use App\Http\Resources\BaseResource;
use App\Models\Article\ArticleModel;
use Illuminate\Http\Resources\MissingValue;

class ArticleListResource extends BaseResource
{
    public function toArray($request): array
    {
        return [
            "id"                  => $this->id,
            "type"                => $this->type,
            "title"               => $this->title,
            "previewUrl"          => json_decode($this->previewUrl, true),
            "coverImgUrl"         => $this->coverImgUrl,
            "description"         => $this->description,
            "supportNum"          => $this->supportNum,
            "collectNum"          => $this->collectNum,
            "forwardNum"          => $this->forwardNum,
            "readNum"             => $this->readNum,
            "showNum"             => $this->showNum,
            "formatEffectiveTime" => formatDate($this->effectiveTime),
            "effectiveTime"       => $this->effectiveTime,
            "status"              => $this->status,
            "statusText"          => translateEnumCodeToCn(ArticleModel::STATUS_TEXT, $this->status, "-"),
            "publishType"         => $this->publishType,
            "publishTypeText"     => translateEnumCodeToCn(ArticleModel::PUBLISH_TYPE_TEXT, $this->publishType, "-"),
            "createTime"          => $this->createdAt->timestamp,
            "formatCreateTime"    => formatDate($this->createdAt->timestamp, "-"),
            "createUsername"      => $this->whenLoaded(
                "createUserRel",
                function () {
                    return vsprintf(
                        "%s(%s)",
                        [
                            attributeGet($this, "createUserRel.name", "-"),
                            attributeGet($this, "createUserRel.nickname", "-")
                        ]
                    );
                },
                new MissingValue()
            ),
            "sort"                => $this->sort,
            "putTop"              => $this->putTop,
            "putHotTop"           => $this->putHotTop,
            "extra"               => $this->extra
        ]; // TODO: Change the autogenerated stub
    }
}
